<?php
namespace Shopmobile\Model;
use Think\Model;
class GiftRightsModel{
	public $sid;  //必输
	public $eid;  //抽奖机会必输
	public $gift; //必输
	public $mygid; //卡密必输
	public $msg;
	public $gift_info;//已发放的奖品信息
	public $hasError;
	public function sendGiftRights()
	{
		$gift = $this->gift;
		$is_send = true;
		
		$gift_type = C("GFT_TYPE");
		foreach($gift_type as $gk=>$gtp){
			if($gtp["id"]==$gift["type"]){
				$this->gift_info["gtype"]=$gk;
			}
		}
		
		switch($gift["type"])
		{
			case 1: //实物
				$is_send=true;
				//抽奖软件不在此检查地址。
				// $ADD = M("UserAddress");
				// $addwh["sid"]=$this->sid;
				// $addwh["shop_uid"]=$this->shop_uid;
				// $address = $ADD->where($addwh)->getField("`id`,`nationality`,`province`,`city`,`city_part`,`address`,`name`,`tel`");
				// if(!$address){
					// $code="need_address";
					// $msg = "您还没有邮寄地址，请输入邮寄地址！";
				// }else{
					// $code="check_address";
					// $msg = "奖品将会邮寄给您。";
				// }
			break;
			case 2: //包邮券
			break;
			case 3: //优惠券
			break;
			case 4: //红包卡密
				//如果分发卡密失败则不予发奖
				if($this->eid==0){$gift["gift_id"]=$gift["id"];} //兼容老版本
				$KM = M("GiftsKm");
				$km = current($KM->where("`sid`=".$this->sid." AND `giftid`=".$gift["gift_id"]." AND `status`=0")->limit(1)->getField("`id`,`cardno`,`secret`"));
				if($km){
					$kmsa["id"]=$km["id"];
					$kmsa["status"]=9;
					$kmsa["shopuid"]=$this->shop_uid;
					$kmsa["user_giftid"]=$this->mygid;
					$KM->save($kmsa);
					$msg .= "卡号：".$km["cardno"]."密码：".$km["secret"]."，您可以在奖品里查看卡密。";
					$is_send=true;
					$this->gift_info["km"]=$km;
				}else{
					$msg .= "奖品已经被领完了！";
					$is_send=false;
				}
			break;
			case 5: //特权折扣
			break;
			case 7: //再来一次
				if($this->eid==0)
				{ // 兼容老版本
					$zailai = M("EventsGifts")->where("id=".$gift["id"])->getField("num_iids");
					
				}
				else
				{
					$MGF = M("Gifts");
					$zailai = $MGF->where("`id`=".$gift["gift_id"])->getField("zailai");
				}
				
				$EVU = M("EventsUsers");
				$evwh["sid"]=$this->sid;
				$evwh["uzuid"]=$this->shop_uid;
				$evwh["eventid"]=$this->eid;
				$EVU->where($evwh)->setInc("score",$zailai);
				$msg .= "恭喜您获得了".$zailai."次抽奖机会！";
				$this->gift_info["score"]=$zailai;
				$this->score = $zailai;
			break;
			case 8: //淘金币
			break;
			case 9: //流量包
			break;
			case 10: //积分
				if($this->eid==0) 
				{// 兼容老版本
					$jifen = M("EventsGifts")->where("id=".$gift["id"])->getField("num_iids");
					
				}
				else
				{
					$MGF = M("Gifts");
					$jifen = $MGF->where("`id`=".$gift["gift_id"])->getField("jifen");
				}
				$JFU = M("ShopUsers");
				$jfw["sid"]=$this->sid;
				$jfw["id"]=$this->shop_uid;
				$JFU->where($jfw)->setInc("seeds",$jifen);
				$this->gift_info["jifen"]=$jifen;
				$this->jifen=$jifen;
			break;
			case "11": //奖章
				if($this->eid==0) 
				{// 兼容老版本
					$mid = M("EventsGifts")->where("id=".$gift["id"])->getField("num_iids");
					
				}
				else
				{
					$MGF = M("Gifts");
					$mid = $MGF->where("`id`=".$gift["gift_id"])->getField("medal_id");
				}
				
				$MET = M("KxdrHonoraryMedalUser");
				$met["sid"]=$this->sid;
				$met["user_id"]=0;
				$met["shop_uid"]=$this->shop_uid;
				$met["medal_id"]=$mid;
				$hasMet = $MET->where($met)->limit(1)->getField("id");
				if(!$hasMet){
					unset($met["id"]);
					$met["get_time"]=date("Y-m-d H:i:s");
					$met["medal_num"]=1;
					$MET->add($met);
				}else{
					$MET->where(Array("id"=>$hasMet))->setInc("medal_num");
				}
				$MED = M("KxdrHonoraryMedal");
				$med = $MED->where(Array("id"=>$mid))->getField("id,title,pic_url");
				$this->gift_info["medal"]=$med;
			break;
			case "6": //其他奖品
			break;
			default:
				$msg .= "领奖成功";
			break;
		}
		if($is_send){
			//标记奖品已经被领取
			
			
		}
		$this->hasError = !$is_send;
		$this->msg = $msg;
	}
}